#!/usr/bin/python
#coding:utf-8

def dns_resolver(name):
	# 本地域名解析
	try:
		import socket
        	results = socket.getaddrinfo(name,None)
		for family, socktype, proto, canonname, sockaddr in results:
			if len(sockaddr) == 2:
				return sockaddr[0]
	except socket.error, e:
		print '本地域名解析错误', e

	# google域名解析
	import os, socket, re
	seqid = os.urandom(2)
	host = ''.join(chr(len(x))+x for x in name.split('.'))
	data = '%s\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00%s\x00\x00\x01\x00\x01' % (seqid, host)
	sock = socket.socket(socket.AF_INET,type=socket.SOCK_DGRAM)
	sock.settimeout(30)
	sock.sendto(data, ('8.8.8.8', 53))
	try:
		data = sock.recv(512)
	except socket.error, e:
		print '8.8.8.8域名解析错误', e
		return name
	iplist = ['.'.join(str(ord(x)) for x in s) for s in re.findall('\xc0.\x00\x01\x00\x01.{6}(.{4})', data) if all(ord(x) <= 255 for x in s)]
	if iplist:
		return iplist[0]
	print '域名解析失败'
	return name
